package cn.gov.msa.util;

import org.springframework.util.StringUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;


public class DateUtils {

	public static final String DF_DATE = "yyyyMMdd";
	public static final String DF_TIME = "yyyyMMddHHmmss";
	public static final String LONG_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String SHOT_FORMAT = "yyyy-MM-dd";
	public static final int ONE_MINUTE = 60;
	public static final int ONE_HOUR = 60 * ONE_MINUTE;
	public static final int ONE_DAY = 24 * ONE_HOUR;
	
	
	
	
	
	public static String getNow() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date());
	}
	public static String getNow(String sdf) {
		return new SimpleDateFormat(sdf).format(new Date());
	}
    /**
     * 日期转字符串
     * @param time 时间
     * @param fomart 格式
     * @return
     */
    public static String date2String (Date time , String fomart){
        if(null == time || null == fomart) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(fomart);
        return format.format(time);
    }
    
    /**
     * 日期转字符串 （默认格式 "yyyy-MM-dd HH:mm:ss"）
     * @param time 时间
     * @return
     */
    public static String date2String (Date time ){
        return date2String(time,"yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 日期转字符串 （默认格式 "yyyy-MM-dd HH:mm:ss"）
     * @param time 时间
     * @return
     * @throws ParseException
     */
    public static Date string2Date (String time, String fomart) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(fomart);
        Date date = format.parse(time);
        return date;
    }


    public static Date string2Date (String time) throws ParseException{
        if(time==null|| StringUtils.isEmpty(time)){
            return null;
        }
        return string2Date(time,"yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获取当天第一秒
     * @return
     */
    public static Date getNowDayBegin(){
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Date d=new Date();
        String str=format.format(d);
        str = str+" 00:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }

    /**
     *
     * @Title: getNowDayBeforeBegin
     * @Description: 前一天的开始时间
     * @date 2016年3月29日 下午4:11:59
     * @author wjp
     *
     * @return
     */
    public static String getNowDayBeforeBegin(){
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        String str=format.format(calendar.getTime());
        str = str+" 00:00:00";

        return str;
    }

    /**
     * 当天最后一秒
     * @return
     */
    public static Date getNowDayEnd(){
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Date d=new Date();
        String str=format.format(d);
        str = str+" 23:59:59";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }

    /**
     * 获取第二天时间：年月日
     * @return
     */
    public static String getNextDay(){
    	
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(new Date());
    	calendar.add(Calendar.DAY_OF_MONTH, 1);
    	
    	return date2String(calendar.getTime(), SHOT_FORMAT);
    }
    
    /**
    *
    * @Title: getNextDayTimeByTime
    * @Description: 得到第二天的指定时间点
    * @param time 第二天的时间点,格式 HH:mm:ss
    * @date 2017-10-17
    * @author yujunxin
    *
    * @return
    */
   public static String getNextDayTimeByTime(String time){
	   String nextDay = getNextDay();
       String str = nextDay + " " + time;
       return str;
   }
    
    /**
     *
     * @Title: getNowDayBeforeBegin
     * @Description: 前一天的结束时间
     * @date 2016年3月29日 下午4:11:59
     * @author wjp
     *
     * @return
     */
    public static String getNowDayEndBegin(){
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        String str=format.format(calendar.getTime());
        str = str+" 23:59:59";

        return str;
    }

    /**
     * @Title: getFirstDayOfMonth
     * @Description: 获得本月第一天 00:00:00
     * @return
     */
    public static String getFirstDayOfMonth(String fomart){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH,1);
        cal.set(Calendar.HOUR_OF_DAY , 0);
        cal.set(Calendar.MINUTE , 0);
        cal.set(Calendar.SECOND , 0);
        return date2String(cal.getTime(), fomart);
    }

    /**
     * @Title: getFirstDayOfMonth
     * @Description: 获得本月第一天
     * @return
     */
    public static Date getFirstDayOfMonth(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH,1);
        cal.set(Calendar.HOUR_OF_DAY , 0);
        cal.set(Calendar.MINUTE , 0);
        cal.set(Calendar.SECOND , 0);

        return cal.getTime();
    }
    /**
     * @Title: getEndDayOfMonth
     * @Description: 获得本月最后一天
     * @return
     */
    public static Date getEndDayOfMonth(){
        Calendar cal = Calendar.getInstance();
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        cal.set(Calendar.HOUR_OF_DAY , 23);
        cal.set(Calendar.MINUTE , 59);
        cal.set(Calendar.SECOND , 59);
        return cal.getTime();
    }



    public static Date getNowDayBegin(int day){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_YEAR , cal.get(Calendar.DAY_OF_YEAR)+day );
        cal.set(Calendar.HOUR_OF_DAY , 0);
        cal.set(Calendar.MINUTE , 0);
        cal.set(Calendar.SECOND , 0);

        return cal.getTime();
    }

    /**
     *
     * @Title: getNowQuarterBegin
     * @Description: 当前季度的开始第一秒
     * @param day
     * @return
     */
    public static Date getNowQuarterBegin(int day){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, day);
        cal.set(Calendar.DAY_OF_MONTH,1);
        cal.set(Calendar.HOUR_OF_DAY , 0);
        cal.set(Calendar.MINUTE , 0);
        cal.set(Calendar.SECOND , 0);
        return cal.getTime();
    }


    public static Date getNowDayEnd(int day){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_YEAR , cal.get(Calendar.DAY_OF_YEAR)+day );
        cal.set(Calendar.HOUR_OF_DAY , 23);
        cal.set(Calendar.MINUTE , 59);
        cal.set(Calendar.SECOND , 59);

        return cal.getTime();
    }

    /**
     *
     * @Title: getNowQuarterBegin
     * @Description: 当前季度的开始这一天的最后一秒
     * @param day
     * @return
     */
    public static Date getNowQuarterEnd(int day){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, day);
        cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY , 23);
        cal.set(Calendar.MINUTE , 59);
        cal.set(Calendar.SECOND , 59);
        return cal.getTime();
    }

    /**
     * 时间加上相应天数
     * getDateByOffsetDay
     * @param date
     * @param days
     * @return
     * @throws Exception
     * @author yws
     */
    public static Date getDateByOffsetDay(Date date,int days){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE,days);
        return calendar.getTime();
    }

    /**
     * 时间加上相应天数
     * getDateByOffsetDay
     * @throws ParseException 
     */
    public static String getDateByOffsetDay(String strDate,int days) throws ParseException{
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=format.parse(strDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE,days);
        return date2String(calendar.getTime());
    }

    
    /**
     * 时间加上相应月份
     * getDateByOffsetDay
     * @param date
     * @param days
     * @return
     * @throws Exception
     * @author yws
     */
    public static Date getDateByOffsetMonth(Date date,int month){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.MONTH,month);
        return calendar.getTime();
    }

    /**
     * 时间加上相应小时
     * getDateByOffsetHours
     * @param date
     * @param hours
     * @return
     * @throws Exception
     * @author yujunxin
     */
    public static Date getDateByOffsetHours(Date date,int hours){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY,hours);
        return calendar.getTime();
    }
    
    /**
     * 时间加上相应分钟
     * getDateByOffsetDay
     * @param date
     * @param days
     * @return
     * @throws Exception
     * @author yws
     */
    public static Date getDateByOffsetMin(Date date,int days){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.MINUTE,days);
        return calendar.getTime();
    }

    /**
     * 时间加上相应秒数
     * getDateByOffsetDay
     * @param date
     * @param days
     * @return
     * @throws Exception
     * @author yws
     */
    public static Date  getDateByOffsetSeconds(Date date,int seconds) throws Exception{
        Calendar calendar1  =   new GregorianCalendar();
        calendar1.setTime(date);
        calendar1.add(Calendar.SECOND,seconds);
        return calendar1.getTime();
    }

    /**
     * 获取指定月第一天
     * @param month
     * @return
     */
    public static Date getFirstDayByMonth(int month){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH,1);
        cal.set(Calendar.HOUR_OF_DAY , 0);
        cal.set(Calendar.MINUTE , 0);
        cal.set(Calendar.SECOND , 0);

        return cal.getTime();
    }


    /**
     * 获取今年指定月最后一天
     * @param month
     * @return
     */
    public static Date getEndDayByMonth(int month){
        Calendar cal = Calendar.getInstance();
        int lastDay = cal.getMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        cal.set(Calendar.HOUR_OF_DAY , 23);
        cal.set(Calendar.MINUTE , 59);
        cal.set(Calendar.SECOND , 59);
        return cal.getTime();
    }
    public static Date getEndDayByMonth(int month,int year){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        cal.set(Calendar.HOUR_OF_DAY , 23);
        cal.set(Calendar.MINUTE , 59);
        cal.set(Calendar.SECOND , 59);
        return cal.getTime();

    }

    /**
     * 获取指定年制定月的第一天
     * @param month
     * @return
     */
    public static Date getFirstDayByMonth(int month,int year){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH,1);
        cal.set(Calendar.HOUR_OF_DAY , 0);
        cal.set(Calendar.MINUTE , 0);
        cal.set(Calendar.SECOND , 0);

        return cal.getTime();
    }

    /**
     * 获取指定年制定月的最后一天
     * @param month
     * @return
     */
    public static Date getEndDayByMonthAndYear(int year,int month){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        cal.set(Calendar.HOUR_OF_DAY , 23);
        cal.set(Calendar.MINUTE , 59);
        cal.set(Calendar.SECOND , 59);
        return cal.getTime();
    }

    /**
     * 获取指定月最后一天
     * @param startTime  08:30
     * 				   endTime   12:30
     * @return
     */
    public static Boolean getResultByStartEnd(String startTime,String endTime){
        if(StringUtils.isEmpty(startTime)||StringUtils.isEmpty(startTime)){
            return false;
        }
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int hour=cal.get(Calendar.HOUR_OF_DAY);//小时
        int minute=cal.get(Calendar.MINUTE);//分
        String _startHour = startTime.substring(0, startTime.indexOf(":"));
        String _startmin = startTime.substring(startTime.indexOf(":")+1);
        int startHour = Integer.parseInt(_startHour);
        int startmin =  Integer.parseInt(_startmin);
        String _endHour = endTime.substring(0, endTime.indexOf(":"));
        String _endmin = endTime.substring(endTime.indexOf(":")+1);
        int endHour = Integer.parseInt(_endHour);
        int endmin =  Integer.parseInt(_endmin);
        String type = "0";
        if(startHour>endHour){
            type ="1";
        }
        else if(startHour == endHour&&startmin>endmin){
            type ="1";
        }
        else{
            type ="0";
        }
        if("0".equals(type)){
            if(hour>startHour&&hour<endHour){
                return true;
            }
            else if(hour==startHour&&minute>=startmin){
                return true;
            }
            else if(hour==endHour&&minute<=endmin){
                return true;
            }
            else{
                return false;
            }
        }
        else{
            if(hour>startHour){
                return true;
            }
            else if(hour<endHour){
                return true;
            }
            else if(hour==startHour&&minute>=startmin){
                return true;
            }
            else if(hour==endHour&&minute<=endmin){
                return true;
            }
            else{
                return false;
            }
        }
    }

    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getFirstDayOfYear(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        //calendar.set(Calendar.DAY_OF_YEAR, 1);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取某年最后一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getLastDayOfYear(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();

        return currYearLast;
    }

    /**
     * 取得两个时间段的时间间隔
     * return t2 与t1的间隔天数
     * throws ParseException 如果输入的日期格式不是0000-00-00 格式抛出异常
     */
    public static Long getBetweenDays(String t1,String t2) throws ParseException{
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Integer betweenDays = 0;
        Date d1 = format.parse(t1);
        Date d2 = format.parse(t2);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        // 保证第二个时间一定大于第一个时间
        if(c1.after(c2)){
            c1 = c2;
            c2.setTime(d1);
        }
        int betweenYears = c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR);
        betweenDays = c2.get(Calendar.DAY_OF_YEAR)-c1.get(Calendar.DAY_OF_YEAR);
        for(int i=0;i<betweenYears;i++){
            int tmp=countDays(c1.get(Calendar.YEAR));
            betweenDays+=countDays(c1.get(Calendar.YEAR));
            c1.set(Calendar.YEAR,(c1.get(Calendar.YEAR)+1));
        }
        return betweenDays.longValue();
    }
    
    /**
	 * 取得两个时间段的时间间隔(小时)
	 * @author yujunxin
	 * @param startTime
	 * @param endTime
	 * @return 
	 */
	public static Long getBetweenHours(Date startTime, Date endTime) {
		long interval = endTime.getTime() - startTime.getTime();
//		interval = interval > 0 ? interval : interval * -1;
		interval = interval / (1000L * ONE_HOUR);
		return interval;
	}
    
    public static int countDays(int year){
        int n=0;
        for (int i = 1; i <= 12; i++) {
            n += countDays(i,year);
        }
        return n;
    }


    public static int countDays(int month, int year){
        int count = -1;
        switch(month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                count = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                count = 30;
                break;
            case 2:
                if(year % 4 == 0)
                    count = 29;
                else
                    count = 28;
                if((year % 100 ==0) & (year % 400 != 0))
                    count = 28;
        }
        return count;
    }

    /**
     * Get the Dates between Start Date and End Date.
     * @param p_start   Start Date
     * @param p_end     End Date
     * @return Dates List
     */
    public static List<Date> getDates(String startTime, String endTime) {
        List<Date> list = new ArrayList<Date>();
        try {
            if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {//为空
                return list;
            }
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date d1 = format.parse(startTime);
            Date d2 = format.parse(endTime);

            if(d1.equals(d2)) { //日期相同
                list.add(d1);

                return list;
            }

            if (d1.after(d2)) { //日期 startTime 在 endTime 之后
                return list;
            }

            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(d1);
            c2.setTime(d2);
            list.add(d1);
            while(c1.before(c2)){

                c1.add(Calendar.DAY_OF_MONTH, 1);
                list.add(c1.getTime());
            }
            //list.add(d2);
        } catch(ParseException e) {
            e.printStackTrace();
        }
        return list;
    }
    /**
     * Get the Months between Start Date and End Date.
     * @param p_start   Start Date
     * @param p_end     End Date
     * @return Dates List
     */
    public static List<Date> getMonths(String startTime, String endTime) {
        List<Date> list = new ArrayList<Date>();
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date d1 = format.parse(startTime);
            Date d2 = format.parse(endTime);
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(d1);
            c2.setTime(d2);
            while(c1.before(c2)){

                c1.add(Calendar.MONTH, 1);
                list.add(c1.getTime());
            }
        } catch(ParseException e) {
            e.printStackTrace();
        }
        return list;
    }
    /**
     * Get the Years between Start Date and End Date.
     * @param p_start   Start Date
     * @param p_end     End Date
     * @return Dates List
     */
    public static List<Date> getYears(String startTime, String endTime) {
        List<Date> list = new ArrayList<Date>();
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date d1 = format.parse(startTime);
            Date d2 = format.parse(endTime);
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(d1);
            c2.setTime(d2);
            while(c1.before(c2)){

                c1.add(Calendar.YEAR, 1);
                list.add(c1.getTime());
            }
        } catch(ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     *
     * @Title: getOneMonthAgoDate
     * @Description: 当前时间获得一个月前
     * @date 2016年3月24日 下午4:53:39
     * @author wjp
     *
     * @return
     * @throws ParseException
     */
    public static String getOneMonthAgoDate(String dateTime) throws ParseException{
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(string2Date(dateTime));
        calendar.add(Calendar.MONTH, -1);    //得到一个月前
        return DateUtils.date2String(calendar.getTime());
    };

    /**
     *  转为 n秒前 n分钟前 n小时前 日期 很长时间前显示的日期格式
     * @author leiza
     * @param time
     * @param format
     * @return
     */
    public static String dataLongToSNS(long time) {
        long now = System.currentTimeMillis();
        long diff = now - time;
        diff = diff / 1000;// 秒

        if (diff < 0) {
            return dateLongToString(time);
        }
        if (diff < 60) { // 60秒 1分钟之内显示 “刚刚”
            return "刚刚";
        }
        if (diff < 3600) {//1分钟~60分钟之内显示 “N分钟前”
            return String.format("%s分钟前", diff / 60);
        }
        // 获取今天凌晨时间
        long todayStart = getMoring(new Date()).getTime();

        if (time >= todayStart ||time <= todayStart) {//大于今天凌晨的时间或者小于今天的时间
            DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = format2.format(new Date(time));
            if(!dateString.contains("2016")){//判断是否包含今年的时间
                return dateLongToString(time, "yyyy-MM-dd HH:mm");
            }
            return dateLongToString(time, "MM-dd HH:mm");//60分钟之后当年显示 “ 03-18 13：00” 格式
        }
        return dateLongToString(time,"yyyy-MM-dd HH:mm");
    }

    // 获取今天凌晨的时间
    private static Date getMoring(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static String dateLongToString(long time) {
        return dateLongToString(time, null);
    }

    public static String dateLongToString(long time, String format) {
        if (time <= 0) {
            return "";
        }
        DateFormat format2 = new SimpleDateFormat(format);
        String dateString = format2.format(new Date(time));
        return dateString;
    }

    /**
     * 获取当天的第一秒，并返回前第三个月的时间，返回字符串 格式:yyyy-MM-dd 00:00:00
     * 如：2016-09-06 18:58:35 则返回 2016-06-06 00:00:00
     * @author leiza
     * @data 2016-09-06
     * @param dateTime
     * @return
     * @throws ParseException
     */
    public static String getThirdMonthAgoDate(String dateTime) throws ParseException{
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Date d=new Date();
        String str=format.format(d);
        str = str+" 00:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -3);//得到3个月前
        return DateUtils.date2String(calendar.getTime());
    };

    /**
     * 获取指定日期下一天
     * @Title:getNext
     * @param date
     * @return
     */
    public static String getNextDay(Date date,String format){
    	SimpleDateFormat dft = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        return dft.format(calendar.getTime());
    }


    /**
     * 获取指定月份的每一天
     * @Title:getMonthAllDays
     * @date 2016-10-12 上午10:27:25
     * @author zrr
     * @return
     */
    public static List<String> getMonthAllDays(int year,int month) {
        List<String> dateList = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        c.setTime(DateUtils.getFirstDayByMonth(month, year));
        int totalDays = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        for(int i=1; i<=totalDays; i++){
            c.set(Calendar.DAY_OF_MONTH, i);
            Date date = c.getTime();
            dateList.add(DateUtils.date2String(date, "yyyy-MM-dd"));
        }
        return dateList;
    }

    /**
     * 获取最近七天
     * @Title:getRecent7Days
     * @date 2016-10-12 上午10:33:44
     * @author zrr
     * @return
     */
    public static List<String> getRecentSevenDays() {
        List<String> dateList = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        for (int i = 6; i >= 0; i--) {
            cal.setTime(new Date());
            cal.add(5, -i);
            dateList.add(date2String(cal.getTime(), "yyyy-MM-dd"));
        }
        return dateList;
    }

    /**
     * 获取最近7天的开始时间
     * @Title:getRecentSevenDaysBegin
     * @date 2016-10-12 下午3:17:49
     * @author zrr
     * @return
     */
    public static Date getRecentSevenDaysBegin() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -6);
        calendar.set(Calendar.HOUR_OF_DAY , 0);
        calendar.set(Calendar.MINUTE , 0);
        calendar.set(Calendar.SECOND , 0);
        return calendar.getTime();
    }

    /**
     * 获取最近30天的开始时间
     * @Title:getRecentSevenDaysBegin
     * @date 2016-10-12 下午3:17:49
     * @author zrr
     * @return
     */
    public static Date getRecentThirtyDaysBegin() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -29);
        calendar.set(Calendar.HOUR_OF_DAY , 0);
        calendar.set(Calendar.MINUTE , 0);
        calendar.set(Calendar.SECOND , 0);
        return calendar.getTime();
    }

    /**
     * 获取前一天开始
     * @Title:getPreDayBegin
     * @date 2016-10-18 下午4:16:53
     * @author zrr
     * @return
     */
    public static Date getPreDayBegin() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY , 0);
        calendar.set(Calendar.MINUTE , 0);
        calendar.set(Calendar.SECOND , 0);
        return calendar.getTime();
    }

    /**
     * 获取上个月的开始时间
     * @Title:getPreDayBegin
     * @date 2016-10-18 下午4:16:53
     * @author zrr
     * @return
     */
    public static Date getPreMonthBegin() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY , 0);
        calendar.set(Calendar.MINUTE , 0);
        calendar.set(Calendar.SECOND , 0);
        return calendar.getTime();
    }

    /**
     * 获取上月的结束时间
     * @Title:getlastDayofMonth
     * @date 2016-10-19 上午8:55:11
     * @author zrr
     * @return
     */
    public static Date getPreMonthEnd(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY , 23);
        calendar.set(Calendar.MINUTE , 59);
        calendar.set(Calendar.SECOND , 59);
        return calendar.getTime();
    }


    public static List<Integer> getIntDateList(Date startDate,Date endDate){
        List<Integer> dateList = new ArrayList<Integer>();
        dateList.add(Integer.parseInt(DateUtils.date2String(startDate, "yyyyMMdd")));
        while (startDate.before(endDate)) {
            Calendar   calendar   =   new   GregorianCalendar();
            calendar.setTime(startDate);
            calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
            startDate=calendar.getTime();   //这个时间就是日期往后推一天的结果
            int result = Integer.parseInt(DateUtils.date2String(startDate, "yyyyMMdd"));
            dateList.add(result);
        }
        return dateList;
    }

    /**
     * 通过分钟数算出当前向上取整时间
     * @param minutes
     * @return
     * @throws Exception
     * @author zkm
     */
    public static Integer getMinutes(Integer minutes) throws Exception{
        Integer num = minutes%30==0?minutes/30:((minutes/30)+1);
        Integer minutesLast=num*30;

        return minutesLast;
    }
    /**
     * 通过传入的时间获取当前的时间信息
     * @param calendarDay
     * @author zkm
     * @return
     * @throws Exception
     */
    public static Map<String, String> getCalendarDay(String calendarDay) throws Exception{
        Map<String,String> map=new HashMap<String, String>();
        Calendar c = Calendar.getInstance();
        c.setTime(DateUtils.string2Date(calendarDay));
        int year = c.get(Calendar.YEAR);    //获取年
        int month = c.get(Calendar.MONTH) + 1;   //获取月份，0表示1月份
        int day = c.get(Calendar.DAY_OF_MONTH);    //获取当前天数
        int hour = c.get(Calendar.HOUR_OF_DAY);       //获取当前小时
        int min = c.get(Calendar.MINUTE);          //获取当前分钟
        int ss = c.get(Calendar.SECOND);          //获取当前秒

        String monthS=month<10?("0"+month):String.valueOf(month);
        String dayS=day<10?("0"+day):String.valueOf(day);

        String date = year+"-"+monthS+"-"+dayS;
        map.put("year", String.valueOf(year));
        map.put("month", String.valueOf(month));
        map.put("day", String.valueOf(day));
        map.put("hour", String.valueOf(hour));
        map.put("min", String.valueOf(min));
        map.put("ss", String.valueOf(ss));
        map.put("date", date);

        return map;
    }
    /**
     * 两个时间差 HH:mm:ss
     * 创建人：wjp
     * 创建时间：2017年4月11日 下午1:58:27
     * @param beginTime
     * @param endTime
     * @return
     */
    public static String getDateToTime(Date beginTime,Date endTime) {
        if (null == beginTime || null == endTime) {
            return "";
        }
        Long duration = endTime.getTime() - beginTime.getTime();

        Long hour = 1000 * 60 *60l;
        Long h = duration / hour ;
        duration -= (hour * h);
        Long m = duration / (1000 * 60);
        duration -= (m * 1000 * 60);
        Long s = duration / (60 * 1000);

        return longToTime(h.toString(), m.toString(), s.toString());
    }
    /**
     * 时分秒转为 HH:mm:ss
     * 创建人：wjp
     * 创建时间：2017年4月11日 下午1:58:08
     * @param h
     * @param m
     * @param s
     * @return
     */
    public static String longToTime(String h,String m,String s) {
        if (h.length() == 1) {
            h = "0" + h;
        }
        if (m.length() == 1) {
            m = "0" + m;
        }
        if (s.length() == 1) {
            s = "0" + s;
        }

        return h + ":" + m + ":" + s;
    }

    /**
     * 将Date类转换为XMLGregorianCalendar
     * @param date
     * @return
     */
    public static XMLGregorianCalendar dateToXmlDate(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        DatatypeFactory dtf = null;
        try {
            dtf = DatatypeFactory.newInstance();
        } catch (DatatypeConfigurationException e) {
        }
        XMLGregorianCalendar dateType = dtf.newXMLGregorianCalendar();
        dateType.setYear(cal.get(Calendar.YEAR));
        //由于Calendar.MONTH取值范围为0~11,需要加1
        dateType.setMonth(cal.get(Calendar.MONTH)+1);
        dateType.setDay(cal.get(Calendar.DAY_OF_MONTH));
        dateType.setHour(cal.get(Calendar.HOUR_OF_DAY));
        dateType.setMinute(cal.get(Calendar.MINUTE));
        dateType.setSecond(cal.get(Calendar.SECOND));
        return dateType;
    }

    /**
     * 将XMLGregorianCalendar转换为Date
     * @param cal
     * @return
     */
    public static Date xmlDate2Date(XMLGregorianCalendar cal){
        return cal.toGregorianCalendar().getTime();
    }

    
    
	public static String getStringDateShort() {
		Date currentTime = new Date();
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	    String dateString = formatter.format(currentTime);
	    return dateString;
	}
	
	public static Date getDate(String string) {
	       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	       Date date = null;
		try {
			date = format.parse(string);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
		
	}
	
	public static Date getDate(String string, String formatStr) {
		if(StringUtils.isEmpty(formatStr)){
			formatStr = LONG_FORMAT;
		}
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		Date date = null;
		try {
			date = format.parse(string);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
		
	}
	
	/** 
	 * 将短时间格式字符串转换为时间 yyyy-MM-dd 
	 *  
	 * @param strDate 
	 * @return 
	 */  
	public static Date strToDate(String strDate) {  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
	    ParsePosition pos = new ParsePosition(0);  
	    Date strtodate = formatter.parse(strDate, pos);  
	    return strtodate;  
	}  
	
	/** 
	 * 将短时间格式时间转换为字符串 yyyy-MM-dd 
	 *  
	 * @param dateDate 
	 * @param k 
	 * @return 
	 */  
	public static String dateToStr(java.util.Date dateDate) {  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
	    String dateString = formatter.format(dateDate);  
	    return dateString;  
	}
	
	public static int daysToSecond(int days){
		return days * ONE_DAY;
		
	}
	
	public static int hoursToSecond(int hours){
		return hours * ONE_HOUR;
	}
	
	public static int minuteToSecond(int minute){
		return minute * ONE_MINUTE;
	}
	
	/**
	 * 获取指定时间的开始时间 格式：yyyy-MM-dd 00:00:00
	 * @param dateTime
	 * @return Date
	 */
	public static Date getOneDateBeginTime(Date dateTime){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTime);
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		return calendar.getTime();
	}
	
    /**
    *
    * 描述:获取下一个月的第一天.
    *
    * @return
    */
   public static String getPerFirstDayOfMonth() {
       SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       Calendar calendar = Calendar.getInstance();
       calendar.add(Calendar.MONTH, 1);
       calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
       return dft.format(calendar.getTime());
   }
   
   /**
   *
   * 描述:获取指定时间的下一个月的第一天00:00:00.
   *
   * @return
   */
  public static String getFirstDayOfNextMonth(Date date) {
	  SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.MONTH, 1);
      calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
      calendar.set(Calendar.HOUR_OF_DAY, 0);  
      calendar.set(Calendar.MINUTE, 0);  
      calendar.set(Calendar.SECOND, 0);  
      return dft.format(calendar.getTime());
  }
   
  /**
  *
  * 描述:获取指定时间的下一个月的第一天00:00:01.
  *
  * @return
 * @throws ParseException 
  */
 public static String getFirstDayOfNextMonth(String strDate) throws ParseException {
	 SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	 Date date = dft.parse(strDate);
	 Calendar calendar = Calendar.getInstance();
	 calendar.setTime(date);
	 calendar.add(Calendar.MONTH, 1);
	 calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
	 calendar.set(Calendar.HOUR_OF_DAY, 0);  
	 calendar.set(Calendar.MINUTE, 0);  
	 calendar.set(Calendar.SECOND, 1);  
	 return dft.format(calendar.getTime());
 }
  
   /**
   *
   * 描述:获取当前系统时间后的N个月的时间
   *
   * @return
   */
   public static  String getDesignationMothDay(String month) {
			//获取当前系统时间后的N个月的时间		
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar c = Calendar.getInstance();
			c.add(Calendar.MONTH, Integer.parseInt(month));
			String packageEndTime =  f.format(c.getTime());
			return packageEndTime;
  }
   
   /**
   *
   * 描述:获取当前系统时间后的N个月的时间
   *
   * @return
   */
   public static  String getDesignationMothDaySubDay(String month) {
			//获取当前系统时间后的N个月的时间		
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar c = Calendar.getInstance();
			c.add(Calendar.MONTH, Integer.parseInt(month));
			c.add(Calendar.DAY_OF_MONTH, -1);
			String packageEndTime =  f.format(c.getTime());
			return packageEndTime;
  }
   
   /**
   *
   * 描述:获取当前系统时间后的N个月的时间的最后一天23:59:59
   *
   * @return
   */
   public static  String getDesignationMothLastDay(String month) {
		//获取当前系统时间后的N个月的时间		
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, Integer.parseInt(month));
		
		 //获取某月最大天数  
       int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);  
       //设置日历中月份的最大天数  
       cal.set(Calendar.DAY_OF_MONTH, lastDay); 
       cal.set(Calendar.HOUR_OF_DAY, 23);  
       cal.set(Calendar.MINUTE, 59);  
       cal.set(Calendar.SECOND, 59);  
		
		String packageEndTime =  f.format(cal.getTime());
		return packageEndTime;
  }
   /**
   *
   * 描述:获取指定时间后的N个月的时间的最后一天23:59:59
   *
   * @return
   */
   public static  String getDesignationMothLastDay(Date date,String month) {
		//获取当前系统时间后的N个月的时间		
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, Integer.parseInt(month));
		
		 //获取某月最大天数  
       int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);  
       //设置日历中月份的最大天数  
       cal.set(Calendar.DAY_OF_MONTH, lastDay); 
       cal.set(Calendar.HOUR_OF_DAY, 23);  
       cal.set(Calendar.MINUTE, 59);  
       cal.set(Calendar.SECOND, 59);  
		
		String packageEndTime =  f.format(cal.getTime());
		return packageEndTime;
  }
   
   
   /**
   *
   * 描述:获取指定时间后的N个月的时间的最后一天23:59:59
   *
   * @return
 * @throws ParseException 
   */
   public static  String getDesignationMothLastDay(String strDate,String month) throws ParseException {
		//获取当前系统时间后的N个月的时间		
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		Date date=f.parse(strDate);
		cal.setTime(date);
		cal.add(Calendar.MONTH, Integer.parseInt(month));
		
		 //获取某月最大天数  
       int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);  
       //设置日历中月份的最大天数  
       cal.set(Calendar.DAY_OF_MONTH, lastDay); 
       cal.set(Calendar.HOUR_OF_DAY, 23);  
       cal.set(Calendar.MINUTE, 59);  
       cal.set(Calendar.SECOND, 59);  
		
		String packageEndTime =  f.format(cal.getTime());
		return packageEndTime;
  }
   
   /**
   *
   * 描述:获取指定时间后的N个月的时间
   *
   * @return
   */
   public static  String getDesignationMothDay(Date date,String month) {
			//获取当前系统时间后的N个月的时间		
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.MONTH, Integer.parseInt(month));
			String packageEndTime =  f.format(calendar.getTime());
			return packageEndTime;
  }
   
   /**
   *
   * 描述:获取指定时间后的N个月的时间 减一秒
   *
   * @return
   */
   public static  String getDesignationMothDaySubDay(Date date,String month) {
			//获取当前系统时间后的N个月的时间		
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.MONTH, Integer.parseInt(month));
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			String packageEndTime =  f.format(calendar.getTime());
			return packageEndTime;
  }
   
   /** 
    * 获取某月的最后一天的23:59:59
    * @Title:getLastDayOfMonth 
    * @Description: 
    * @param:@param year 
    * @param:@param month 
    * @param:@return 
    * @return:String 
    * @throws 
    */  
   public static String getLastDayOfMonth(int year,int month)  
   {  
       Calendar cal = Calendar.getInstance();  
       //设置年份  
       cal.set(Calendar.YEAR,year);  
       //设置月份  
       cal.set(Calendar.MONTH, month-1);  
       //获取某月最大天数  
       int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);  
       //设置日历中月份的最大天数  
       cal.set(Calendar.DAY_OF_MONTH, lastDay); 
       cal.set(Calendar.HOUR_OF_DAY, 23);  
       cal.set(Calendar.MINUTE, 59);  
       cal.set(Calendar.SECOND, 59);  
       
       //格式化日期  
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
       String lastDayOfMonth = sdf.format(cal.getTime());  
         
       return lastDayOfMonth;  
   }  
   
	/**
	 * 获取某月的最后一天的 23:59:59
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static String getLastDayOfMonth(String time) throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(DateUtils.string2Date(time));
		// 获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		// 设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);

		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String lastDayOfMonth = sdf.format(cal.getTime());
		return lastDayOfMonth;
	}
   
   /** 
    * 获取某月的第一天的00:00:00
    * @Title:getLastDayOfMonth 
    * @Description: 
    * @param:@param year 
    * @param:@param month 
    * @param:@return 
    * @return:String 
    * @throws 
    */  
   public static String getFirstDayOfMonth(int year,int month)  
   {  
       Calendar cal = Calendar.getInstance();  
       //设置年份  
       cal.set(Calendar.YEAR,year);  
       //设置月份  
       cal.set(Calendar.MONTH, month-1);  
       //获取某月最大天数  
       int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);  
       //设置日历中月份的最大天数  
       cal.set(Calendar.DAY_OF_MONTH, 1); 
       cal.set(Calendar.HOUR_OF_DAY, 0);  
       cal.set(Calendar.MINUTE, 0);  
       cal.set(Calendar.SECOND, 0);  
       
       //格式化日期  
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
       String lastDayOfMonth = sdf.format(cal.getTime());  
         
       return lastDayOfMonth;  
   }  
   
   /** 
    * 获取两个时间的中间年月
    * @Title:getLastDayOfMonth 
    * @Description: 
    * @param:@param year 
    * @param:@param month 
    * @param:@return 
    * @return:String 
    * @throws 
    */  
   
   //param:2016 5  2018 3
   //return:[{"year":2016,"month":5},{"year":2016,"month":6},{"year":2016,"month":7},{"year":2016,"month":8},{"year":2016,"month":9},{"year":2016,"month":10},{"year":2016,"month":11},{"year":2016,"month":12},{"year":2017,"month":1},{"year":2017,"month":2},{"year":2017,"month":3},{"year":2017,"month":4},{"year":2017,"month":5},{"year":2017,"month":6},{"year":2017,"month":7},{"year":2017,"month":8},{"year":2017,"month":9},{"year":2017,"month":10},{"year":2017,"month":11},{"year":2017,"month":12},{"year":2018,"month":1},{"year":2018,"month":2},{"year":2018,"month":3}]

   public static JSONArray getBetweenYearAndMonth(int beginYear,int beginMonth,int endYear,int endMonth)  
   {  
	  
	   
	   
	   
	   JSONArray result=new JSONArray();
	   for (; beginYear<endYear || beginMonth<= endMonth &&beginYear==endYear; beginYear+=beginMonth/12 , beginMonth = beginMonth%12+1) {
		   JSONObject o= new JSONObject();
		   o.put("year", beginYear);  
		   o.put("month", beginMonth); 
		   result.add(o);
	   }
//       Calendar beginCal = Calendar.getInstance();  
//       //设置年份  
//       beginCal.set(Calendar.YEAR,beginYear);  
//       //设置月份  
//       beginCal.set(Calendar.MONTH, beginMonth-1); 
//       //获取某月最大天数  
//       int lastDay = beginCal.getActualMaximum(Calendar.DAY_OF_MONTH);  
//       //设置日  
//       beginCal.set(Calendar.DAY_OF_MONTH, lastDay);      
//       beginCal.set(Calendar.HOUR_OF_DAY, 0);  
//       beginCal.set(Calendar.MINUTE, 0);  
//       beginCal.set(Calendar.SECOND, 0);     
//       Calendar endCal = Calendar.getInstance();  
//       //设置年份  
//       endCal.set(Calendar.YEAR,endYear);  
//       //设置月份  
//       endCal.set(Calendar.MONTH, endMonth-1);
//       lastDay = endCal.getActualMaximum(Calendar.DAY_OF_MONTH);  
//       //设置日  
//       endCal.set(Calendar.DAY_OF_MONTH, lastDay); 
//       endCal.set(Calendar.HOUR_OF_DAY, 0);   
//       endCal.set(Calendar.MINUTE, 0);  
//       endCal.set(Calendar.SECOND, 0);  
//       while(beginCal.getTime().before(endCal.getTime())){
//    	  JSONObject o= new JSONObject();
//    	  o.put("year", beginCal.get(Calendar.YEAR));  
//    	  o.put("month", beginCal.get(Calendar.MONTH)+1);  
//    	  result.add(o);
//    	  beginCal.add(Calendar.MONTH, 1);
//       }  
       return result;  
   }  
   
   private static List<Map<String, String>> calYearAndMonth(String packageActiveTime) throws ParseException {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		Calendar calActive = Calendar.getInstance();
		calActive.setTime(DateUtils.string2Date(packageActiveTime));
		if (calActive.get(Calendar.DAY_OF_MONTH) >= 27){
			calActive.add(Calendar.MONTH, 1);
		}
		calActive.set(Calendar.DAY_OF_MONTH, 26);
		calActive.set(Calendar.HOUR_OF_DAY, 0);
		calActive.set(Calendar.MINUTE, 0);
		calActive.set(Calendar.SECOND, 0);
		calActive.set(Calendar.MILLISECOND, 0);
		while (calActive.before(cal)) {
			calActive.add(Calendar.MONTH, -1);
			Map<String, String> map = new HashMap<String, String>();
			map.put("year", calActive.get((Calendar.YEAR)) + "");
			map.put("month", calActive.get((Calendar.MONTH)) + 1 + "");
			list.add(map);
			calActive.add(Calendar.MONTH, 2);
		}
		return list;
	}
   public static int compare_date(String DATE1, String DATE2) {
       DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       try {
           Date dt1 = df.parse(DATE1);
           Date dt2 = df.parse(DATE2);
           if (dt1.getTime() > dt2.getTime()) {
               return 1;
           } else if (dt1.getTime() < dt2.getTime()) {
               return -1;
           } else {
               return 0;
           }
       } catch (Exception exception) {
           exception.printStackTrace();
       }
       return 0;
   }
  
   public static void 	main(String[] args) throws Exception {
	   
	   
	    
	    String oldPackageActiveTime = "2018-04-03 00:00:01";
		Map<String, String> effectiveMap =  DateUtils.getDesignationYearMoth(oldPackageActiveTime);
		int effectiveYear = Integer.parseInt(effectiveMap.get("year"));
		int effectiveMonth = Integer.parseInt(effectiveMap.get("month"));
		 String oldPackageEndTime = "2018-9-30 23:59:59";
		Map<String, String> nowMap = DateUtils.getDesignationYearMoth(DateUtils.getNow());
		Map<String, String> endMap = DateUtils.getDesignationYearMoth(oldPackageEndTime);
		int endYear = Integer.parseInt(endMap.get("year"));
		int endMonth = Integer.parseInt(endMap.get("month"));
		
		int nowYear = Integer.parseInt(nowMap.get("year"));
		int nowMonth = Integer.parseInt(nowMap.get("month"));
		int usedMonths = DateUtils.getBetweenYearAndMonth(effectiveYear,effectiveMonth, nowYear, nowMonth).size();
		int residualMonths = DateUtils.getBetweenYearAndMonth(nowYear, nowMonth, endYear, endMonth).size();
		System.out.println(usedMonths+""+residualMonths);
		BigDecimal usedMoney = new BigDecimal(
				"3").subtract(
						new BigDecimal("3")
								.multiply(new BigDecimal(usedMonths))
								.divide(new BigDecimal("6"), 0,
										BigDecimal.ROUND_UP));
		System.out.println(usedMoney);
		BigDecimal addMoney = new BigDecimal("5").multiply(new BigDecimal(residualMonths)).divide(new BigDecimal("6"), 0,BigDecimal.ROUND_UP).subtract(usedMoney).setScale(2, BigDecimal.ROUND_HALF_UP);
		System.out.println(addMoney);
   }
   /**
   *
   * 描述:获取指定时间后的N个月的时间
   *
   * @return
   */
   public static  String getDesignationTime(Date date , String month) {
			//获取当前系统时间后的N个月的时间		
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(Calendar.MONTH, Integer.parseInt(month));
			String packageEndTime =  f.format(c.getTime());
			return packageEndTime;
  }
   /**
    * 获取本月每一天 截止到当天
    * @Title:getNowMonthDay
    * @date 2016-10-9 下午2:24:43
    * @author zrr
    * @return
    */
   public static List<String> getNowMonthAllDays() {
       List<String> dateList = new ArrayList<>();
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
       Date d = new Date();
       Date date = DateUtils.getFirstDayOfMonth();
       while (!date.after(d)) {
           dateList.add(sdf.format(date));
           date = getNextDay(date);
       }
       return dateList;
   }
   /**
    * 获取下一天
    * @Title:getNext
    * @date 2016-10-9 上午11:42:49
    * @author zrr
    * @param date
    * @return
    */
   public static Date getNextDay(Date date){
       Calendar calendar = Calendar.getInstance();
       calendar.setTime(date);
       calendar.add(Calendar.DATE, 1);
       return calendar.getTime();
   }
   /**
   *
   * 描述:获取指定时间的年份和月份
   *
   * @return map集合
   */
   public  static Map<String, String> getDesignationYearMoth(String date) throws ParseException {
		Map<String, String> map = new HashMap<String, String>();
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date time =sdf.parse(date);
		calendar.setTime(time);
		map.put("year", calendar.get((Calendar.YEAR))+"");
		map.put("month", calendar.get((Calendar.MONTH))+1+"");
		return map;
	}
   /**
   *
   * 描述:获取指定时间的年份和月份
   *
   * @return map集合
   */
   public  static Map<String, String> getDesignationYearMoth(Date date) throws ParseException {
	   	Map<String, String> map = new HashMap<String, String>();
	   	Calendar calendar = Calendar.getInstance();
	   	calendar.setTime(date);
		map.put("year", calendar.get((Calendar.YEAR))+"");
		map.put("month", calendar.get((Calendar.MONTH))+1+"");
		return map;
	}

}
